package com.swq.java.code.chapter18.code09;

import java.io.*;

public class PrintStreamTester {
    private static void readBuff(byte[] buff) throws IOException {
        ByteArrayInputStream in = new ByteArrayInputStream(buff);
        int data;
        while ((data = in.read()) != -1)
            System.out.print(data + " ");
        System.out.println();
        in.close();
    }

    public static void main(String agrs[]) throws IOException {
        //通过PrintStream写字符串“好”
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        PrintStream ps = new PrintStream(out, true);
        ps.print("好");
        ps.close();

        byte[] buff = out.toByteArray(); //获得字节数组
        System.out.println("采用本地操作系统的默认字符编码：");
        readBuff(buff);

        //通过DataOutputStream写字符串“好”
        out = new ByteArrayOutputStream();
        DataOutputStream ds = new DataOutputStream(out);
        ds.writeUTF("好");
        ds.close();

        buff = out.toByteArray(); //获得字节数组
        System.out.println("采用适用于Java语言的UTF-8字符编码：");
        readBuff(buff);
    }
}
